<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property int $id 
 * @property int $category_id 
 * @property string $industry 
 * @property int $goods_type 
 * @property string $title 
 * @property string $caption 
 * @property float $price 
 * @property float $original_price 
 * @property string $picture 
 * @property string $banners 
 * @property int $is_marketable 
 * @property int $sort 
 * @property int $sales 
 * @property int $view_num 
 * @property int $stock 
 * @property int $disable 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $deleted_at
 * @property GoodsContents $contents
 */

class Goods extends Model
{
    use SoftDeletes;

    public $dateFormat = 'U';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'goods';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'category_id' => 'integer', 'goods_type' => 'integer', 'price' => 'float', 'original_price' => 'float', 'banners' => 'array','is_marketable' => 'integer', 'sort' => 'integer', 'sales' => 'integer', 'view_num' => 'integer', 'stock' => 'integer', 'disable' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'integer'];

    /**
     * 商品详情关联模型
     *
     * @return hasOne
     */
    public function contents(): HasOne
    {
        return $this->hasOne(GoodsContents::class, 'goods_id');
    }

    /**
     * SKU 关联模型
     *
     * @return hasMany
     */
    public function sku(): HasMany
    {
        return $this->hasMany(GoodsSku::class, 'goods_id');
    }

    /**
     * 商品属性关联模型
     */
    public function property()
    {
        return $this->hasMany(GoodsProperty::class, 'goods_id');
    }

    /**
     * 封面图片获取器
     *
     * @param $value
     * @return string
     */
    public function getPictureAttribute($value)
    {
        return gConfig('ali_oss_domain') . $value;
    }
}